package tx.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tx.dao.BookDao;

//@Transactional(readOnly = true)//只读
//@Transactional(timeout = 3),超时效果，单位是秒
//@Transactional(noRollbackFor = ArithmeticException.class)//某种类型的异常不进行回滚
//@Transactional(isolation = Isolation.REPEATABLE_READ)//设置隔离级别
@Transactional(propagation = Propagation.REQUIRED)
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao bookDao;

    @Override
    public void buyBook(int bookId, int userId) {
        //根据id查询图书价格
        int price = bookDao.getBookPrice(bookId);
        //更新图书库存-1
        bookDao.updateStock(bookId);
        //更新用户余额-价格
        bookDao.updateUserBalance(userId,price);
    }
}
